package com.hatic.process;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;

/**
 * 项目信息处理类。
 *
 * @author TonyTan
 * @version 1.0, 2011-10-21
 */
public class ProjectBaseBO extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	//项目立项提交时检查必须填写项目负责人
	public String infoCheck(String[] projectIds) {
		if (projectIds == null || projectIds.length == 0) {
			setMessage("没有选择项目记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = projectIds.length; i < n; i++) {
			Map<String,String> mpData = queryProject(projectIds[i]);
			String project_user = mpData.get("project_user");
			
			if (project_user.length() == 0) {
				String projectCode = mpData.get("project_code");
				setMessage("{0}项目的项目负责人没有选择，不能提交！", projectCode);
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}

	//项目批复信息提交
	public String postAudit(String[] projectIds) {
		if (projectIds == null || projectIds.length == 0) {
			setMessage("没有选择项目记录！");
			return _returnFaild;
		}
		
		for (int i = 0, n = projectIds.length; i < n; i++) {
			if (returnPlan(projectIds[i]).equals(_returnFaild)) {
				setMessage("立项批复信息更新到投资计划中出错，不能提交！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 负责反馈项目信息到投资计划中；
	 * 并需要把项目文号、项目金额反馈到投资计划包中；
	 * 修改投资计划的状态为已实施；
	 * @param planId
	 * @return
	 */
	public String returnPlan(String projectId) {
		Map<String, String> plandata = queryPlanId(projectId);
		if (plandata.isEmpty()) {
			setMessage("没有找到有效的投资计划");
			return _returnFaild;
		}
		String planId = plandata.get("plan_id");
		String parentId = plandata.get("plan_parentid");
		
		Map<String,String> project = queryProject(projectId);
		String project_fileno = project.get("project_fileno");
		String project_checkname = project.get("project_checkname");
		String check_dept = project.get("check_dept");
		String check_date = project.get("check_date");
		String project_money = project.get("project_money");
		String done_deptid = project.get("done_deptid");
		String done_deptname = project.get("done_deptname");
		
		if (check_date.length() == 0) {
			setMessage("项目批复日期不能为空！");
			return _returnFaild;
		}
		
		if (!updatePlan(project_fileno, project_checkname, check_dept, check_date, project_money, 
				planId, done_deptid, done_deptname)) {
			setMessage("更新投资计划中的项目批复信息出错！");
			return _returnFaild;
		}
		
		//如果是包内计划，则需要反馈投资计划中的项目批复信息
		String bao_type = project.get("bao_type");
		if (parentId.length() > 0 && bao_type.equals("6")) {
			Map<String, String> parentdata = queryPlan(parentId);
			String money = parentdata.get("project_money");
			
			//累计投资金额
			double summoney = Double.parseDouble(project_money) + Double.parseDouble(money);
			
			if (!updatePlan(project_fileno, project_checkname, check_dept, check_date, Double.toString(summoney), parentId, 
					done_deptid, done_deptname)) {
				setMessage("更新投资计划包中的项目批复信息出错！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}
	
	//更新投资计划中的项目批复信息
	private boolean updatePlan(String fileno, String filename, String checkdept, 
			String check_date, String money, String planId, String done_deptid, String done_deptname) {
		String sql = "update money_plan set project_fileno = ?, project_checkname = ?, " +
					 "project_checkdept = ?, project_checkdate = ?, project_money = ?, " +
					 "done_deptid = ?, done_deptname = ?, plan_status = '42' " +
					 "where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(fileno);
		param.addStringValue(filename);
		param.addStringValue(checkdept);
		param.addDateValue(check_date);
		param.addDoubleValue(money);
		param.addStringValue(done_deptid);
		param.addStringValue(done_deptname);
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	//取项目基础信息
	public Map<String,String> queryProject(String projectId) {
		String sql = "select * from project_base where project_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		
		return _dao.queryMap(param);
	}
	
	/**
	 * 根据项目ID取投资计划ID，考虑到投资计划会结转，所以才根据项目ID去找投资计划ID
	 * @param projectId
	 * @return
	 */
	public Map<String, String> queryPlanId(String projectId) {
		String sql = "select plan_id, plan_parentid from money_plan where plan_status < '71' and project_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		
		return _dao.queryMap(param);
	}
	
	//取投资计划信息
	private Map<String, String> queryPlan(String planId) {
		String sql = "select * from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		return _dao.queryMap(param);
	}
}
